﻿using Hangfire;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.BackgroundWorkers.Hangfire;
using Volo.Abp.Modularity;

namespace Colorful.Furniture.BackgroundWorkerManagement;

/// <summary>
///  后台服务
/// </summary>
[DependsOn(
typeof(AbpBackgroundWorkersHangfireModule))]
public class BackgroundWorkerManagementModule : AbpModule
{
    /// <summary>
    ///  配置服务
    /// </summary>
    /// <param name="context"></param>
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        ConfigureHangfire(context, configuration);
    }

    /// <summary>
    ///  配置Hangfire
    /// </summary>
    /// <param name="context"></param>
    /// <param name="configuration"></param>
    private void ConfigureHangfire(ServiceConfigurationContext context, IConfiguration configuration)
    {
        context.Services.AddHangfire(config =>
        {
            config.UseSqlServerStorage(configuration.GetSection("ConnectionString:Default").Value);
        });
    }

    /// <summary>
    ///  程序启动时候
    /// </summary>
    /// <param name="context"></param>
    public override async Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();

        ////注册到后台工作者管理器
        //await context.AddBackgroundWorkerAsync<StatisticsWorker>();

        //使用控制台
        app.UseHangfireDashboard(options: new DashboardOptions
        {
            Authorization = new[] { new HangfireAuthorizationFilter() }
        });
    }
}